<template>
  <div>
    <e-table
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="state" label="状态" width="80" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state === 0">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleBecomeUser(scope.row)">
                <a href="javascript:;">成为该用户</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="$emit('setRole', scope.row)">
                <a href="javascript:;">设置角色</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleReset(scope.row)">
                <a href="javascript:;">重置密码</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red" href="javascript:;">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { getUserList, deleteUser, resetPassword } from '@/api/manage'
import { openNotification, setSession, welcome } from '@/utils/util'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { mapActions } from 'vuex'
import axios from 'axios'

export default {
  components: {},
  name: 'UserTable',
  data() {
    return {
      // 查询参数
      queryParam: {
        state: 0,
        pageNo: 1,
        pageSize: 10
      },
      loading: true,
      // 表头
      columns: [
        { slot: 'order' },
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '用户名',
          prop: 'username'
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '所属部门',
          prop: 'deptName'
        },
        {
          label: '角色分配',
          prop: 'roleName'
        },
        { slot: 'state' },
        { slot: 'action' }
      ],
      tableData: [],
      total: 1
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    ...mapActions(['Logout']),
    indexMethod(index) {
      return (this.queryParam.pageNo - 1) * this.queryParam.pageSize + index + 1
    },
    // 成为该用户处理
    async handleBecomeUser(row) {
      console.log('row', row)
      let uri = process.env.NODE_ENV === 'production' ? process.env.VUE_APP_API_BASE_URL : '/api/yb'
      const post = { userId: row.userId }

      this.$store
        .dispatch('changeUser', post)
        .then(() => {
          openNotification('success', '更换用户成功')
          setTimeout(() => {
            window.location.href = '/'
          }, 500)
        })
        .catch(err => {
          openNotification('error', '请求出错')
        })
    },
    handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      getUserList(this.queryParam).then(res => {
        if (res && res.data.records.length > 0) {
          const {
            state,
            data: { records, total }
          } = res
          this.tableData = records
          this.total = total
        } else {
          this.tableData = []
          this.total = 1
        }
        this.loading = false
      })
    },
    handleEdit(record) {
      this.$nextTick(() => {
        this.$emit('edit', record)
      })
    },
    // 重置密码
    handleReset(row) {
      const _this = this
      this.$confirm({
        title: '重置操作',
        content: `您确定将【${row.name}】的密码重置吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          const res = await resetPassword({ userId: row.userId })
          openNotification('success', '重置操作成功', '密码已重置为123456')
        }
      })
    },
    // 删除
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除【${row.name}】这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          const res = await deleteUser(row.userId, {})
          openNotification('success', '删除操作成功')
          _this.handleGetList()
        }
      })
    }
  }
}
</script>
